#include<bits/stdc++.h>
using namespace std;
const int N=4e6+5;
int n,k,x,cnt,tot,siz[N],ans=2e9+5;
struct node{
	int id,num;
	bool operator<(const node &x)const{
		return num<x.num;
	}
}a[N];
inline void add(int x){
	if(!siz[a[x].id]) ++tot;
	++siz[a[x].id];
}
inline void del(int x){
	if(!x) return;
	--siz[a[x].id];
	if(!siz[a[x].id]) --tot;
}
template <class T=int>
inline T read() {
	char c=getchar();
	bool sign=(c=='-');
	while(c<'0'||c>'9')
	c=getchar(),sign|=(c=='-');
	T x=0;
	while('0'<=c&&c<='9')
	x=(x<<1)+(x<<3)+(c&15),c=getchar();
	return sign?(~x+1):x;
}
signed main() {
	freopen("contest.in","r",stdin);
	freopen("contest.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	n=read();
	for(int i=1;i<=n;i++){
		k=read();
		while(k--) x=read(),a[++cnt]={i,x};
	}
	sort(a+1,a+cnt+1);
	int L=0;
	for(int i=1;i<=cnt;i++){
		del(i-1);
		while(tot!=n){
			if(L==cnt) break;
			add(++L);
		}
		if(tot!=n) break;
		ans=min(ans,a[L].num-a[i].num);
	}
	cout<<ans;
	return 0;
}
